home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter10 / TargetViewpoint1TestPanel.java < prev    next >
Text File  |  2000-09-08  |  2KB  |  98 lines

  1. package applets;
  2.  
  3. import shout3d.*;
  4. import shout3d.core.*;
  5. import shout3d.math.*;
  6. import custom_nodes.*;
  7.  
  8.  
  9. public class TargetViewpoint1TestPanel extends Shout3DPanel implements DeviceObserver {
  10.    
  11.    
  12.    int pixelStartX;
  13.    int pixelStartY;
  14.    int pixelEndX;
  15.    int pixelEndY;
  16.  
  17.    TargetViewpoint1 cam;
  18.    
  19.  
  20.    public TargetViewpoint1TestPanel(Shout3DApplet applet){
  21.       super(applet);
  22.    }
  23.       
  24.    public void customInitialize() {
  25.         
  26.       addDeviceObserver(this,"MouseInput", null);
  27.       cam = (TargetViewpoint1) getCurrentBindableNode("Viewpoint");
  28.    
  29.    }
  30.  
  31.  
  32.    protected void finalize()  { 
  33.       removeDeviceObserver(this,"MouseInput");
  34.    }
  35.  
  36.    
  37.    
  38.    public boolean onDeviceInput(DeviceInput di, Object userData) {
  39.       MouseInput mi = (MouseInput) di;
  40.       switch (mi.which){
  41.  
  42.          case MouseInput.DOWN:
  43.             pixelStartX = mi.x;
  44.             pixelStartY = mi.y;
  45.             return true;
  46.  
  47.          case MouseInput.DRAG:
  48.  
  49.            //if left button used
  50.            if(mi.button == 0) {
  51.  
  52.             pixelEndX = mi.x;
  53.             pixelEndY = mi.y;
  54.  
  55.             int dragDistanceX = pixelEndX - pixelStartX;
  56.             int dragDistanceY = pixelEndY - pixelStartY;
  57.             
  58.             //convert drag to rotation
  59.             //at 70 pixels per radian (57.3 degrees)
  60.             float headingDelta = dragDistanceX/70f;
  61.             float pitchDelta = dragDistanceY/70f;
  62.             
  63.             //compute new heading and pitch
  64.             float temp = cam.heading.getValue() - headingDelta;
  65.             cam.heading.setValue(temp);
  66.             temp = cam.pitch.getValue() - pitchDelta;
  67.             cam.pitch.setValue(temp);
  68.    
  69.             pixelStartY = pixelEndY;
  70.             pixelStartX = pixelEndX;
  71.       
  72.             return true;
  73.  
  74.             }//end of 0 if
  75.  
  76.            //if right button used
  77.            if(mi.button == 1) {
  78.  
  79.             pixelEndY = mi.y;
  80.             int dragDistanceY = pixelEndY - pixelStartY;
  81.             
  82.             float distanceDelta = dragDistanceY/150f;
  83.             float temp = cam.distance.getValue() + distanceDelta;
  84.             cam.distance.setValue(temp);
  85.  
  86.             pixelStartY = pixelEndY;
  87.             return true;
  88.  
  89.             }//end of 1 if
  90.          
  91.       }//end of switch
  92.  
  93.       return false;
  94.  
  95.    }
  96.  
  97.  
  98. }//end of class